package com.bj58.mis.bigdata.mdp.sys.controller;

import com.bj58.bsp.v2.user.vo.LoginUserBaseInfo;
import com.bj58.bsp.v2.utils.ServletLoginUtil;
import com.bj58.mis.bigdata.mdp.sys.common.consts.ResultCodeConsts;
import com.bj58.mis.bigdata.mdp.sys.common.result.ResultBean;
import com.bj58.mis.bigdata.mdp.sys.util.JsonUtil;
import com.bj58.mis.bigdata.mdp.sys.vo.LoginUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bspSSO")
public class BspSSOController extends SSOController {



    @GetMapping("/login")
    public ResultBean login(HttpServletRequest request){
        LoginUserBaseInfo loginUserBaseInfo = ServletLoginUtil.getLoginUser(request);

        if(loginUserBaseInfo!=null){
            LoginUser loginUser = buildLoginUser(loginUserBaseInfo);
            String token = saveLoginUserToRedis(loginUser);
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("loginUser", loginUser);
            return ResultBean.success(data, "登录成功!");
        }else{
            return ResultBean.error("未登录！");
        }
    }

    private LoginUser buildLoginUser(LoginUserBaseInfo loginUserBaseInfo) {
        LoginUser loginUser = new LoginUser();
        loginUser.setId(loginUserBaseInfo.getId());
        loginUser.setUserName(loginUserBaseInfo.getUsername());
        loginUser.setRealName(loginUserBaseInfo.getRealname());
        loginUser.setOrgId(loginUserBaseInfo.getOrgId());
        loginUser.setEmail(loginUserBaseInfo.getEmail());
        return loginUser;
    }

    @GetMapping("/currLoginUser")
    public ResultBean currLoginUser(HttpServletRequest request){
        LoginUserBaseInfo loginUserBaseInfo = ServletLoginUtil.getLoginUser(request);

        if(loginUserBaseInfo!=null){
            LoginUser loginUser = buildLoginUser(loginUserBaseInfo);
            return ResultBean.success(loginUser, "已登录!");
        }else{
            return ResultBean.error("未登录！");
        }

    }

}
